package com.anda.jsu.yangsp.core.sys.logger;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 
 * @author xiaolu
 * @since 2012-12-06
 *
 */
@Component
@Aspect
public class MainInvokingLoggerUtil {

	Logger logger = null;

	@Pointcut("execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.*(..))")
	public void recordWrite() {
	}

	@Around("recordWrite()")
	public Object mainOperation(ProceedingJoinPoint joinPoint){
		Object object = null;
		String targetClazz = joinPoint.getTarget().toString();
		Class<?> clazz;
		try {
			clazz = Class.forName(targetClazz.substring(0, targetClazz.indexOf("@")));
			logger = Logger.getLogger(clazz);
			logger.error("访问类:" + clazz.getName());
			logger.error("访问方法:" + joinPoint.getSignature().getName());
			if(joinPoint.getArgs().length != 0){
				logger.error("参数:" + joinPoint.getArgs()[0].toString());
			}
			object = joinPoint.proceed();
			logger.error("返回结果:" + object.toString());
			logger.error("----------------------------------------------------------------------");
		} catch (ClassNotFoundException e) {
			AndaLogger.recordLogger(e.getMessage());
		} catch (Throwable e) {
			AndaLogger.recordLogger(e.getMessage());
		}
		return object;
	}

}
